package com.tc.tickets.push;
//package main.java.com.tc.tickets.push;


import android.content.Context;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.text.TextUtils;


import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * YaoWen(43194) create at tongcheng work pc,
 * time:  2017/3/1 11:01  qq:2669932513
 */
class JPushUtil {

    private static final String PUSH_SP_NAME = "push_sp";
    private static final String PUSH_ALIAS_KEY = "push_alias_key";

    /**
     * 检测 是否联网
     * @param context
     * @return
     */
    public static boolean isConnected(Context context){
        ConnectivityManager conn = (ConnectivityManager)context.getSystemService(context.CONNECTIVITY_SERVICE);
        NetworkInfo info = conn.getActiveNetworkInfo();
        return (info!=null && info.isConnected());
    }

    /**
     * 校验Tag Alias 只能是数字,英文字母和中文
     * @param s
     * @return
     */
    public static boolean isValidTagAndAlias(String s) {
        if (TextUtils.isEmpty(s)){
            return false;
        }

        Pattern p = Pattern.compile("^[\u4E00-\u9FA50-9a-zA-Z_!@#$&*+=.|]+$");
        Matcher m = p.matcher(s);
        return m.matches();
    }

    public static void saveAliasToSp(Context context, String alias){
        SharedPreferences sp = context.getSharedPreferences(PUSH_SP_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putString(PUSH_ALIAS_KEY, alias);
        editor.apply();
    }

    public static String getAliasFromSp(Context context){
        SharedPreferences sp = context.getSharedPreferences(PUSH_SP_NAME, Context.MODE_PRIVATE);
        String alias = sp.getString(PUSH_ALIAS_KEY, "");
        return alias;
    }


}
